package com.hailiang.dom;

import com.hailiang.unit.DBUnit;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class UnameQuery {

    //查询账号是否存在
    public boolean queryName(String uname) throws Exception {
        System.out.println(uname);
        boolean bb =true;
        if(uname!=null && uname.length()!=0) {
            Connection conn = DBUnit.getConn();
            PreparedStatement ps = conn.prepareStatement("select * from userinfo where username=" + "uname");
            bb = ps.executeQuery().next();
        }else{
            bb = false;
        }
       return  bb;
    }
//   向数据库插入数据
    public int fengzhuangData(String uname,String pword,String pnumber) throws Exception {
        QueryRunner queryRunner = DBUnit.getQr();
        Connection conn = DBUnit.getConn();

//        String sql = "insert into userinfo values(?,?,?,?)";
//        String str[] = {null,"uname","pword","pnuber"};
        PreparedStatement ps = conn.prepareStatement("insert into userinfo values(null," + uname + "," + pword + "," + pnumber + ")");
        int i = ps.executeUpdate();
        return i;
    }

    //根据账号查询密码是否匹配
    public String queryPwordFromUname(String name) throws Exception {
        Connection conn = DBUnit.getConn();
        PreparedStatement ps = conn.prepareStatement("select password from userinfo where username=?");
        ps.setString(1,""+name+"");
        ResultSet rs = ps.executeQuery();

            String string = rs.getString(0);

        return string;
    }


}
